package com.suhe.stock.http;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.suhe.stock.entity.StockKLine;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 拉取单只股票日K数据
 */
public class StockKLineHttp {

    private final static String URL = "https://push2his.eastmoney.com/api/qt/stock/kline/get?secid={}&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1,f2,f3,f4,f5,f6&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&klt=101&fqt=1&end=20500101&lmt=10000&_=1723603925138";

    public static List<StockKLine> getData(String code) throws IOException {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(StrUtil.format(URL, code));
        CloseableHttpResponse response = httpClient.execute(httpGet);

        int statusCode = response.getStatusLine().getStatusCode();

        HttpEntity entity = response.getEntity();
        String body = EntityUtils.toString(entity);

        JSONObject jsonObject = JSONUtil.parseObj(body);

        //关闭资源
        response.close();
        httpClient.close();

        String _code = jsonObject.getJSONObject("data").getStr("code");
        String _name = jsonObject.getJSONObject("data").getStr("name");
        JSONArray klines = jsonObject.getJSONObject("data").getJSONArray("klines");

        return klines.stream().map(kline -> {
            String[] items = kline.toString().split(",");
            LocalDate date = LocalDateTimeUtil.parseDate(items[0]);
            Double open = Double.valueOf(items[1]);
            Double close = Double.valueOf(items[2]);
            Double high = Double.valueOf(items[3]);
            Double low = Double.valueOf(items[4]);
            Double volume = Double.valueOf(items[5]);
            Double turnover = Double.valueOf(items[6]);
            Double amplitude = Double.valueOf(items[7]);
            Double upRate = Double.valueOf(items[8]);
            Double up = Double.valueOf(items[9]);
            Double changes = Double.valueOf(items[10]);

            return new StockKLine()
                    .setCode(_code)
                    .setName(_name)
                    .setDate(date)
                    .setOpen(open)
                    .setClose(close)
                    .setHigh(high)
                    .setLow(low)
                    .setVolume(volume)
                    .setTurnover(turnover)
                    .setAmplitude(amplitude)
                    .setUpRate(upRate)
                    .setUp(up)
                    .setChanges(changes);
        }).collect(Collectors.toList());

    }
}
